<template>
  <div class="login">
    <div class="loginBox">
      <el-form label-position="left" label-width="0px" class="card-box login-form">
        <p class="title">系统登录</p>
        <el-form-item prop="wechat">
          <el-input name="email" type="text" v-model="wechat" autocomplete="on" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            type="password"
            v-model="password"
            autocomplete="on"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { login } from '@/api'
import Cookies from 'js-cookie'

export default {
  name: 'login',
  data() {
    return {
      loginFlag: false,
      wechat: '18865311618',
      password: '123456'
    }
  },
  created() {
    if (Cookies.get('token')) {
      this.$router.replace({ path: '/index' })
    }
    document.onkeydown = (e) => {
      if (this.loginFlag) {
        return false
      }
      if (e.keyCode === 13) {
        this.onSubmit()
      }
    }
  },
  methods: {
    onSubmit() {
      if (!this.username && !this.password) {
        this.$message('请将帐号密码填写完整')
      } else {
        login({
          wechat: this.wechat,
          password: this.password
        }).then((res) => {
          console.log(res)
          Cookies.set('token', res.token)
          let path = this.$store.state.lastPath
          this.$router.replace({ path: path ? path : '/index' })
          // window.location.reload()
        }).catch(() => {
          this.$notify({
            title: '警告',
            message: '用户名或密码错误',
            type: 'warning'
          })
        })
      }
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.login
  position: fixed
  width: 100%
  height: 100%
  left: 0
  top: 0
  background: url(../assets/bg.jpg) no-repeat center center
  background-size: cover
  .loginBox
    display: flex
    justify-content: center
    margin-top: 15%
    padding: 0 10%
    .bg_img img
      left: 500px
      height: auto
    .el-form
      background: rgba(255, 255, 255, 0.3)
      padding: 20px 20px 25px
      border-radius: 10px
      margin-left: 80px
      .title
        font-size: 18px
        margin: 5px 0 20px
        color: #fff
      .el-button
        width: 100%
</style>
